/*
 * @Description:
 * @Version: 2.0
 * @Autor: zhangjx
 * @Date: 2023-03-31 11:58:58
 * @LastEditors: uphldjx 1373577124@qq.com
 * @LastEditTime: 2024-07-01 14:09:29
 */
import apiMod from '@/apiMod/index'
import { useCrudStore } from '@/stores/crud'
import { getStore } from '@/utils/store'
export default defineComponent({
	name: 'AplidAreaSelect',
	props: {
		modelValue: {
			type: Array,
		},
		modelName: {
			type: String,
		},
		regionGrade: {
			type: Number,
		},
		administrationPid: {
			type: Number,
		},
		checkStrictly: {
			type: Boolean,
			default: true,
		},
		isNotShowProvince: {
			type: Boolean,
			default: false,
		},
		authArea: {
			type: Boolean,
			default: false,
		},
		authNextArea: {
			type: Boolean,
			default: false,
		},
		getAllName: {
			type: Boolean,
			default: false,
		},
	},
	emits: ['change', 'update:modelValue', 'update:modelName', 'update:modelOther'],
	setup(props, { slots, attrs, emit }) {
		const { areaTreeData, setMenuTreeData } = useCrudStore()
		const cascaderRef = ref()
		const userInfo = getStore('userInfo') || {}
		const cascaderMod = ref([])
		const handleChange = (e) => {
			const nodes = cascaderRef.value!.getCheckedNodes() || []
			let names = []
			let value = []
			let dataItem = {}
			if (nodes.length > 0) {
				names = nodes[0].pathLabels
				value = nodes[0].pathValues
				dataItem = nodes[0].data
			}
			if (attrs.propOther) {
				const code = nodes[0]?.data?.administrationCode
				emit('update:modelOther', code)
			}
			let pathLabs = names.join('/')
			if (props.regionGrade) {
				// if(!props.getAllName){
				const labels = nodes.filter((item) => item.level <= props.regionGrade).map((item) => item.text)
				pathLabs = labels.join('/')
				// }
			}
			emit('change', { names, value, dataItem }, value)
			emit('update:modelValue', cascaderMod.value)
			if (attrs.propName) emit('update:modelName', pathLabs)
		}
		let options = ref([])
		const defaultProps = {
			...attrs,
			checkStrictly: props.checkStrictly,
			multiple: attrs.multiple,
			expandTrigger: 'click' as const,
			value: 'administrationId',
			label: 'administrationName',
		}

		const getInitData = () => {
			if (areaTreeData.length == 0) {
				let query = {
					regionGrade: props.regionGrade,
					administrationPid: props.administrationPid,
					isNotShowProvince: props.isNotShowProvince,
				}
				if (props.authArea && !query.administrationPid) {
					const authAreaArr = [userInfo.authProvince, userInfo.authCity, userInfo.authArea, userInfo.authStreet, userInfo.authCommunity].filter(
						Boolean
					)
					if (props.authNextArea) {
						query.administrationPid = authAreaArr.slice(-1)[0]
					}

					if (!props.modelValue || props.modelValue.length == 0) {
						cascaderMod.value = authAreaArr
					}
				}
				apiMod.sysadministration_getTree(query).then((res) => {
					options.value = res.data
					setMenuTreeData(res.data)
					if (props.authArea) {
						setTimeout(() => {
							handleChange(cascaderMod.value)
						}, 0)
					}
				})
			} else {
				options.value = areaTreeData
			}
		}

		watch(
			() => props.modelValue,
			() => {
				cascaderMod.value = props.modelValue
			}
		)

		watch(
			() => props.regionGrade,
			() => {
				getInitData()
			}
		)
		onMounted(() => {
			cascaderMod.value = props.modelValue
			getInitData()
		})

		return () => {
			return (
				<el-cascader
					v-model={cascaderMod.value}
					ref={cascaderRef}
					class="w-100"
					tag-type="success"
					options={options.value}
					props={defaultProps}
					clearable
					filterable
					onChange={(e) => handleChange(e)}
				/>
			)
		}
	},
})
